<?php
//require '../../Library/mythumbnail.class.php';
class myViewer_folder{
	public $path;
	public $url;
	//private $alias;
	private $pattern;
	private $enableThumbnails = false;
	public function __construct($path,$url)
	{
		$this->path($path)->url($url);
	}

	public function url($string = null)
	{
		$this->url = rtrim(rtrim($string,DIRECTORY_SEPARATOR),'/');
		return $this;
	}
	public function path($string = null)
	{
		$this->path = rtrim(rtrim($string,DIRECTORY_SEPARATOR),'/');
		return $this;
	}
	public function enableThumbnails($true_or_false = true)
	{
		$this->enableThumbnails = $true_or_false;
		return $this;
	}
	public function isThumbnail(){
		return $this->enableThumbnails;
	}
	public function disableThumbnails($true_or_false = true)
	{
		$this->enableThumbnails(!$true_or_false);
		return $this;
	}
	public function createThumb($file,$size = '48X48')
	{
		if($this->enableThumbnails):
		$thumb_dir = $this->path . DIRECTORY_SEPARATOR . '.thumbs';
		$thumb_file = $thumb_dir . DIRECTORY_SEPARATOR . $size .'' .$file;
		if(!is_dir($thumb_dir))	{	mkdir($thumb_dir);	}
		if(!file_exists($thumb_file))
		{
			$imagen = myThumbnail::newInstance($this->path .'/'. $file);
			$imagen->resize(48,48);
			$imagen->save($thumb_file);
		}
		return $this->url . '/.thumbs/'.$size .'' .$file;
		endif;
		
	}
	/*public function alias($string = null)
	{
		$this->path = $string;
		return $this;
	}/**/
	public function pattern($string = '*.{txt,json}')
	{
		$this->pattern = $string;
		return $this;
	}
	public function return_glob()
	{
		//return new GlobIterator($this->path . DIRECTORY_SEPARATOR . $this->pattern);
		//echo $this->path . DIRECTORY_SEPARATOR . $this->pattern;
		return glob($this->path . DIRECTORY_SEPARATOR . $this->pattern,GLOB_BRACE);
	}
}
/**
 * @version 1.0
 */
class myViewer
{
	//private $rootName = ''; //Nombre del directorio raiz
	private $extensionsPermited = array();
	private $actual = '';
	private $folders = array(); //array('myExample' => 'c:/myExample');
	private $files = array();
	private $urls = array();
	private $json = array('folders'=>array(),'files' => array());
	private $action = '';
	private $open = '';
	private $active_thumb = false;
	private $folder_open = '';
	private $file_open = '';
	private $enableThumbnails = false;
	private $run = null;
	//private $folders
	//public function __construct($rootName = 'myViewer')
	public function __construct()
	{
		//$this->rootName = $rootName;
		$p = explode(':',@$_GET['myviewer_action'],2);
		$this->action = $p[0];
		if(isset($p[1]))
		{
			$this->open = $p[1];
			$p = explode('/',trim($p[1],'/'),2);
			$this->folder_open = $p[0];
			if(isset($p[1])){
				$this->file_open = $p[1];
			}
		}
		
	}
	public function __destruct(){
		if(!is_null($this->run) && !$this->run){$this->process();}
	}
	public function enableThumbnails($true_or_false = true)
	{
		$this->enableThumbnails = $true_or_false;
		return $this;
	}
	public function disableThumbnails($true_or_false = true)
	{
		$this->enableThumbnails(!$true_or_false);
		return $this;
	}
	public function extensions($extensions)
	{
		if(is_array($extensions)){
			$this->extensionsPermited = $extensions;
		}
		else if(is_string($extensions))
		{
			$this->extensionsPermited = func_get_args();
		}
		return $this;
	}
	public function folder($alias = null,$path,$url)
	{
		$this->run = false;
		/*if(is_string($alias))
		{

		}/**/
		$nombre = is_string($alias)?$alias:basename($path);
		$this->folders[$nombre] = new myViewer_folder($path,$url);
		if($this->enableThumbnails){
			$this->folders[$nombre]->enableThumbnails();
		}
		return $this->folders[$nombre]
			->pattern('*.{'.implode(',', $this->extensionsPermited).'}')
			//->alias($nombre)
			;
		/*$this->folders[$nombre]
		implode(',',$this->extensionsPermited)
		$this->folders[$nombre] = $path;
		$this->urls[$nombre] = $url;/**/
	}
	/*public function folders($array){

	}/**/
	public function process()
	{
		switch($this->action)
		{
			case 'folders':
				$this->json['folders'] = array();
				foreach ($this->folders as $alias => $ruta_actual) {
					$this->json['folders'][] = $alias;
				}
			break;
			case 'files':
			/*foreach ($this->folders as $alias => $class_folder) {
				
			}/**/
				if(isset($this->folders[$this->folder_open]))
				{
					$folder = $this->folders[$this->open];
					foreach ($folder->return_glob() as $path) {
						$file = basename($path);
						$this->json['files'][] = 
						array($file,$folder->url . '/.thumbs/48X48' .$file );

					}
					$this->json['url'] = $folder->url;
					if(!$folder->isThumbnail()){
						$this->json['no_thumbnail'] = 1;
					}
				}
			break;
			case 'thumb':
				if(isset($this->folders[$this->folder_open]))
				{
					$folder = & $this->folders[$this->folder_open];
					//var_dump(expression)
					$this->json['thumb'] = $folder->createThumb($this->file_open);
					///$folder->path
				}
			break;
			
		}
		echo json_encode($this->json);
		$this->run = true;
	}
	public function getFolters()
	{
		$this->json['folders'] = array();
	}
	public function getFiles()
	{
		$this->json['files'] = array();

		//array('files' => )
	}
}
/*$test = 'a:b-c';
switch(true)
{
	case isset($test):
		$piezas = explode(':',$test);
		echo $piezas[0];echo "\n";
	case isset($piezas[1]):
		echo $piezas[1];
		echo "\n";
		$dos = explode(':',$piezas[1]);
	case is_string($dos) === true:var_dump($dos);
	//break;
	case 1==1:
	echo "se salto algo";
	break;
}
die();/**//*
if(!isset($_GET['myviewer_action']))
{
$_GET['myviewer_action'] = 'files:muestra';
}*/
require_once './../permisos.php';
$my = new myViewer('Imagenes');
$my->enableThumbnails()->extensions('txt','jpg','png','jpeg');
//$my->folder('muestra',__DIR__ ,'http://folder');
$my->folder('imagenes_de_ejemplo',dirname(__FILE__) .'/examples'  ,
	BASE_URL.'/portafolio/programacion/visor_archivos/examples');

$my->process();
//sleep(1);
//$my->folder($nombre);
//var_dump(glob(__DIR__. '/*.{php,json,class}',GLOB_BRACE|GLOB_MARK ));/**/